<!DOCTYPE html>
<html class="x-admin-sm">

<head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.2</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="stylesheet" href="./css/font.css">
    <link rel="stylesheet" href="./css/xadmin.css">
    <script src="./lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="./js/xadmin.js"></script>
    <script type="text/javascript" src="js/gainID.js"></script>
    <script type="text/javascript" src="js/jquery-1.8.3.js"></script>
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
    <style>
        #show {
            cursor: pointer;
        }

        .login-container {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }
    </style>
</head>

<body>
<div style="display: none;" class="login-container" id="updateUserInfo">
    <form class="layui-form" id="user">
        <div class="layui-form-item">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-inline">
                <input type="text" name="username" readonly required lay-verify="required" lay-reqtext="请输入用户名"
                       placeholder="请输入用户名" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">密码</label>
            <div class="layui-input-inline">
                <input type="password" name="password" required lay-verify="required" lay-reqtext="请输入密码"
                       placeholder="请输入密码" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">性别</label>
            <div class="layui-input-inline">
                <select name="sex" lay-verify="sex" id="sex">
                    <option value="1">请选择性别</option>
                    <option value="男">男</option>
                    <option value="女">女</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">出生日期</label>
            <div class="layui-input-inline">
                <input type="text" name="time" id="time" required lay-verify="required|date" lay-reqtext="请输入日期"
                       placeholder="请输入日期" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">身份证</label>
            <div class="layui-input-inline">
                <input type="text" name="identityCard" required lay-verify="required|identity"
                       lay-reqtext="请输入身份证"
                       placeholder="请输入身份证" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">电话</label>
            <div class="layui-input-inline">
                <input type="text" name="phone" required lay-verify="required|phone" lay-reqtext="请输入电话"
                       placeholder="请输入电话" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">所在单位</label>
            <div class="layui-input-inline">
                <input type="text" name="unit" lay-reqtext="请输入所在单位"
                       placeholder="请输入所在单位" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">地址</label>
            <div class="layui-input-inline">
                <input type="text" name="address" lay-reqtext="请输入地址"
                       placeholder="请输入地址" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">角色</label>
            <div class="layui-input-inline">
                <select name="jobType" lay-verify="jobType" id="jobType">
                    <option value="0">请选择角色</option>
                    <option value="1">军事迷</option>
                    <option value="2">评论员</option>
                    <option value="3">指挥员</option>
                    <option value="4">情报收集者</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">邮箱</label>
            <div class="layui-input-inline">
                <input type="text" name="email" lay-reqtext="请输入邮箱"
                       placeholder="请输入邮箱" lay-verify="email" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-inline">
                <input type="text" name="remarks" lay-reqtext="请输入备注"
                       placeholder="请输入备注" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" id="addButton" lay-submit lay-filter="updateUser" type="submit">修改</button>
                <button type="reset" class="layui-btn">重置</button>
            </div>
        </div>
    </form>
</div>
<div class="x-nav">
			<span class="layui-breadcrumb">
				<a href="">首页</a>
				<a>
					<cite>导航元素</cite></a>
			</span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
       onclick="location.reload()" title="刷新">
        <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
    </a>
</div>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">
                    <button class="layui-btn" onclick="xadmin.open('添加用户','./admin-add.html',600,400)"><i
                            class="layui-icon"></i>添加
                    </button>
                </div>
                <div style="cursor: pointer" class="layui-card-body layui-table-body layui-table-main">
                    <table class="layui-hide" id="userInfo" lay-filter="pageTable"></table>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
<script>
    layui.use(['table'], function () {
        let table = layui.table;
        let form = layui.form;


        // 创建渲染实例
        table.render({
            elem: '#userInfo',
            url: '/user/userPage', // 此处为静态模拟数据，实际使用时需换成真实接口
            page: true,
            cols: [
                [
                    {
                        field: 'id',
                        fixed: 'left',
                        align: 'center',
                        title: 'ID',
                        sort: true
                    },
                    {
                        field: 'username',
                        align: 'center',
                        title: '用户名',
                    },
                    {
                        field: 'password',
                        align: 'center',
                        title: '密码',
                    },
                    {
                        field: 'sex',
                        align: 'center',
                        title: '性别',
                    },
                    {
                        field: 'time',
                        align: 'center',
                        title: '出生日期',
                    },
                    {
                        field: 'identityCard',
                        align: 'center',
                        title: '身份证',
                    },
                    {
                        field: 'phone',
                        align: 'center',
                        title: '联系电话',
                    },
                    {
                        field: 'unit',
                        align: 'center',
                        title: '所在单位',
                    },
                    {
                        field: 'address',
                        align: 'center',
                        title: '家庭地址',
                    },
                    {
                        field: 'jobType',
                        align: 'center',
                        title: '角色',
                        templet: function (d) {
                            if (d.jobType == 1) {
                                return '<span class="layui-btn layui-btn-normal layui-btn-mini">军事迷</span>';
                            } else if (d.jobType == 2) {
                                return '<span class="layui-btn">评论员</span>';
                            } else if (d.jobType == 3) {
                                return '<span class="layui-btn layui-btn-danger">指挥员</span>';
                            } else {
                                return '<span class="layui-btn layui-btn-warm">情报收集者</span>';
                            }
                        }
                    },
                    {
                        field: 'email',
                        align: 'center',
                        title: '邮箱',
                    },
                    {
                        field: 'remarks',
                        align: 'center',
                        title: '备注',
                    },
                    {
                        field: 'state',
                        align: 'center',
                        title: '状态',
                        templet: function (d) {
                            if (d.state == 1) {
                                return '<span class="layui-btn layui-btn-normal layui-btn-mini">正常</span>'
                            } else {
                                return '<span class="layui-btn layui-btn-normal layui-btn-danger">封禁</span>'
                            }
                        }
                    },
                    {
                        field: 'username',
                        align: 'center',
                        title: '操作',
                        templet: '#barEdit'
                    },
                ]
            ],
            limits: [1, 3, 5, 7, 9],
            limit: 3,
            done: function (res, curr, count) {
                $("[lay-id='userInfo']>.layui-table-page").css({
                    "text-align": "center"
                });
            }
        });

        layui.form.verify({
            sex: function (value, item) {
                if (value == 1) {
                    return '请选择性别';
                }
            },
            jobType: function (value, item) {
                if (value == 0) {
                    return '请选择权限';
                }
            }
        });

        let openUpdateUserInfo = null;
        let userId = 0;
        table.on('tool(pageTable)', function (obj) {
            let data = obj.data,
                layEvent = obj.event;
            switch (layEvent) {
                case 'del':
                    layer.confirm('确定要删除吗', function (index) {
                        if (index != 0) {
                            $.get('/user/deleteUser', {"id": data.id}, (result) => {
                                if (result.state == 200) {
                                    layer.msg(result.message, {
                                        icon: 1,
                                        time: 3000
                                    });
                                    table.reload('userInfo');
                                    layer.close(index);
                                } else {
                                    layer.msg(result.message, {
                                        icon: 2,
                                        time: 3000
                                    });
                                    table.reload('userInfo');
                                    layer.close(index);
                                }
                            });
                        }
                    });
                    break;
                case 'state':
                    if (data.state == 1) {
                        layer.confirm('确定要封禁吗', function (index) {
                            if (index != 0) {
                                $.post('/user/userState', {"state": 2, "id": data.id}, (result) => {
                                    if (result.state == 200) {
                                        layer.msg(result.message, {
                                            icon: 1,
                                            time: 3000
                                        });
                                        table.reload('userInfo');
                                        layer.close(index);
                                    } else {
                                        layer.msg(result.message, {
                                            icon: 2,
                                            time: 3000
                                        });
                                        table.reload('userInfo');
                                        layer.close(index);
                                    }
                                });
                            }
                        });
                    } else {
                        layer.confirm('确定要恢复吗', function (index) {
                            if (index != 0) {
                                $.post('/user/userState', {"state": 1, "id": data.id}, (result) => {
                                    if (result.state == 200) {
                                        layer.msg(result.message, {
                                            icon: 1,
                                            time: 3000
                                        });
                                        table.reload('userInfo');
                                        layer.close(index);
                                    } else {
                                        layer.msg(result.message, {
                                            icon: 2,
                                            time: 3000
                                        });
                                        table.reload('userInfo');
                                        layer.close(index);
                                    }
                                });
                            }
                        });
                    }
                    break;
                case 'edit':
                    userId = data.id;
                    $("input[name=username]").val(data.username);
                    $("input[name=password]").val(data.password);
                    $("input[name=time]").val(data.time);
                    $("input[name=identityCard]").val(data.identityCard);
                    $("input[name=phone]").val(data.phone);
                    $("input[name=unit]").val(data.unit);
                    $("input[name=address]").val(data.address);
                    $("input[name=email]").val(data.email);
                    $("input[name=remarks]").val(data.remarks);
                    openUpdateUserInfo = layer.open({
                        type: 1,
                        title: '修改用户信息',
                        content: $("#updateUserInfo"),
                        area: ['900px', '600px'],
                        shadeClose: false,
                        end: function () {
                            $("#updateUserInfo").hide();
                        }
                    });
                    break;
            }
        });
        form.on('submit(updateUser)', function (data) {
            let user = $("#user").serialize();
            $.post('/user/updateUserInfo?id=' + userId, user, (result) => {
                if (result.state == 200) {
                    layer.msg(result.message, {
                        icon: 1,
                        time: 3000
                    });
                    table.reload('userInfo');
                    layer.close(openUpdateUserInfo);
                } else {
                    layer.msg(result.message, {
                        icon: 2,
                        time: 3000
                    });
                    table.reload('userInfo');
                    layer.close(openUpdateUserInfo);
                }
            })
            return false;
        });

    });
</script>

<script type="text/html" id="barEdit">
    <span id="show">
			<i class="layui-icon" lay-event="edit">&#xe642;</i>
			<i class="layui-icon" lay-event="del">&#xe640;</i>
			<i class="layui-icon" lay-event="state">&#xe631;</i></span>
</script>

<script type="text/html" id="listCRUD">

    <a onclick="openUrl('修改','./admin-modify.html',600,400,'{{d.id}}','{{d.username}}','{{d.password}}','{{d.sex}}','{{d.time}}','{{d.identityCard}}','{{d.phone}}','{{d.unit}}','{{d.address}}','{{d.jobType}}','{{d.email}}','{{d.remarks}}','{{d.state}}')"
       title="修改" href="javascript:;">
        <i class="layui-icon">&#xe631;</i>
    </a>
    <a title="删除" onclick="member_del(this,'{{d.salary_id}}')" href="javascript:;">
        <i class="layui-icon">&#xe640;</i>
    </a>
</script>
<script>
    var _hmt = _hmt || [];
    (function () {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?b393d153aeb26b46e9431fabaf0f6190";
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(hm, s);
    })();
</script>

</html>
